import Taro, { Component } from '@tarojs/taro'
import PropTypes from 'prop-types'
import { View, Text, Image } from '@tarojs/components'
import { AtAvatar } from 'taro-ui'
import util from '../../utils/util';


if (process.env.TARO_ENV === "weapp") {
  require("taro-ui/dist/weapp/css/index.css")
} else if (process.env.TARO_ENV === "h5") {
  require("taro-ui/dist/h5/css/index.css")
}

import './index.less'

export default class OrderList extends Component {

  constructor(props) {
    super(props)
    this.state = {
      time:''
    }
  }

  AskingQuestions(orderNum) {
    Taro.navigateTo({
      url:'/pages/DoctorOrderDetail/index?orderNum='+ orderNum
    })
  }

  times(e){
    if(e.orderStatus == 2){
      if(e.firstQuestionContent !== undefined && e.secondQuestionContent == undefined){
        return '第1次提问'
      }else if(e.secondQuestionContent !== undefined && e.thirdQuestionContent == undefined){
        return '第2次提问'
      }else if(e.thirdQuestionContent !== undefined){
        return '第3次提问'
      }
    }
  }

  //医生倒计时
  doctorTimeDown = (e) => {
    if(e !== undefined){
      // setTimeout(this.doctorTimeDown,1000);
      // this.setState({
      //   time:util.timeDown(e)
      // })
      // return this.state.time.hou + '小时' + this.state.time.min + '分'
      return util.timeDown(e).hou + '小时' + util.timeDown(e).min + '分'
    }
  }

  render() {
    const { data } = this.props
    return (
      <View>
        <View className='list'>
          {data.map(item => (
            <View className='list-item' key={item.id} onClick={this.AskingQuestions.bind(this,item.orderNum)}>
              {item.orderStatus == "1" && Taro.getStorageSync('loginInfo').role == 1 && <View className='list-item-label list-item-label-wait'>
                待接诊
              </View>
              }
              {item.orderStatus == "2" && Taro.getStorageSync('loginInfo').role == 1 && <View className='list-item-label list-item-label-now'>
                接诊中
              </View>
              }
              {item.orderStatus == "3" && <View className='list-item-label list-item-label-finish'>
                已结束
              </View>
              }
              {item.orderStatus == "4" && <View className='list-item-label list-item-label-finish'>
                关闭
              </View>
              }
              <View className='list-item-top'>
                <View className='at-row at-row__align--center'>
                  <View className='at-col at-col-12 align--center'>
                    <AtAvatar circle image={item.headImage} size='small'></AtAvatar>
                    {
                      Taro.getStorageSync('loginInfo').role == 1
                      ? <View>
                         <Text className='list-item-name'>{item.doctorName}</Text>
                         <Text className='list-item-level'>{item.doctorTitle}</Text>
                        </View>
                      : <View className='list-item-doctorBox'>
                         <View className='list-item-name'>{item.nickName}</View>
                         {
                           item.orderStatus == 2 &&
                           <View>{this.times(item)}</View>
                         }
                         {
                           item.orderStatus == 1 &&
                           <View>{this.doctorTimeDown(item.firstQuestionTime)}后关闭</View>
                         }
                        </View>
                    }
                  </View>
                </View>
              </View>

                {
                  item.firstQuestionContent !== undefined && item.secondQuestionContent == undefined &&
                  <View>
                    <View className='list-item-main'>
                      {item.firstQuestionContent}
                    </View>
                    <View className='list-item-bottom'>
                      {util.formatDateTime(item.firstQuestionTime)}
                    </View>
                  </View>
                }
                {
                  item.secondQuestionContent !== undefined && item.thirdQuestionContent == undefined &&
                  <View>
                    <View className='list-item-main'>
                      {item.secondQuestionContent}
                    </View>
                    <View className='list-item-bottom'>
                      {util.formatDateTime(item.secondQuestionTime)}
                    </View>
                  </View>
                }
                {
                  item.thirdQuestionContent !== undefined &&
                  <View>
                    <View className='list-item-main'>
                      {item.thirdQuestionContent}
                    </View>
                    <View className='list-item-bottom'>
                      {util.formatDateTime(item.thirdQuestionTime)}
                    </View>
                  </View>
                }
            </View>
          ))}
        </View>
      </View>
    )
  }
}

OrderList.propTypes = {
  data: PropTypes.array
}
